/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package flightlifter;

import java.util.Vector;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;

/**
 *
 * @author Matt
 */
public class FlightCam extends Node {

    Point forwardNode;
    Point middleNode;
    Point topNode;
    PVector up = new PVector(), right = new PVector(), forward = new PVector();
    int vw, vh;  //viewport dimensions.  This should always be full screen.

    public FlightCam(Point forwardNode, Point middleNode, Point topNode, int vw, int vh) {
        this.forwardNode = forwardNode;
        this.middleNode = middleNode;
        this.topNode = topNode;

        this.vw = vw;
        this.vh = vh;

        physics();
    }

    public void camera() {


        GL11.glViewport(0, 0, vw, vh);
        GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
        GL11.glLoadIdentity(); // Reset The Projection Matrix

        GLU.gluPerspective(
                90.0f,
                (float) vw / vh,
                1f,
                25000.0f);


        GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
        GL11.glLoadIdentity();



        GLU.gluLookAt(middleNode.pos.x, middleNode.pos.y, middleNode.pos.z,
                forwardNode.pos.x, forwardNode.pos.y, forwardNode.pos.z,
                up.x, up.y, up.z);


        GLProject.getMatrices();
      
    }

    public void physics() {
        PVector.sub(forwardNode.pos, middleNode.pos, forward);
        forward.normalize();

        PVector.sub(topNode.pos, middleNode.pos, up);
        up.normalize();

        PVector.cross(forward, up, right);

        right.normalize();

    }

    public void render(int mode) {
        GL11.glColor3f(0f, 0f, 1f);

        GL11.glLineWidth(2.f);
        GL11.glBegin(GL11.GL_LINE_LOOP);                    // Drawing Using Triangles


        GL11.glVertex3f(forwardNode.pos.x + up.x + right.x,
                forwardNode.pos.y + up.y + right.y,
                forwardNode.pos.z + up.z + right.z);
        GL11.glVertex3f(forwardNode.pos.x + up.x - right.x,
                forwardNode.pos.y + up.y - right.y,
                forwardNode.pos.z + up.z - right.z);
        GL11.glVertex3f(forwardNode.pos.x - up.x - right.x,
                forwardNode.pos.y - up.y - right.y,
                forwardNode.pos.z - up.z - right.z);
        GL11.glVertex3f(forwardNode.pos.x - up.x + right.x,
                forwardNode.pos.y - up.y + right.y,
                forwardNode.pos.z - up.z + right.z);
        GL11.glVertex3f(forwardNode.pos.x + up.x + right.x,
                forwardNode.pos.y + up.y + right.y,
                forwardNode.pos.z + up.z + right.z);
        GL11.glVertex3f(middleNode.pos.x, middleNode.pos.y, middleNode.pos.z);
        GL11.glVertex3f(forwardNode.pos.x + up.x - right.x,
                forwardNode.pos.y + up.y - right.y,
                forwardNode.pos.z + up.z - right.z);
        GL11.glVertex3f(middleNode.pos.x, middleNode.pos.y, middleNode.pos.z);
        GL11.glVertex3f(forwardNode.pos.x - up.x - right.x,
                forwardNode.pos.y - up.y - right.y,
                forwardNode.pos.z - up.z - right.z);
        GL11.glVertex3f(middleNode.pos.x, middleNode.pos.y, middleNode.pos.z);
        GL11.glVertex3f(forwardNode.pos.x + up.x + right.x,
                forwardNode.pos.y + up.y + right.y,
                forwardNode.pos.z + up.z + right.z);
        GL11.glVertex3f(middleNode.pos.x, middleNode.pos.y, middleNode.pos.z);
        GL11.glVertex3f(forwardNode.pos.x - up.x + right.x,
                forwardNode.pos.y - up.y + right.y,
                forwardNode.pos.z - up.z + right.z);

        GL11.glEnd();
    }

    public FlightCam(String[] line, Vector<Node> nodes, int vw, int vh) {
        this((Point) nodes.elementAt(Integer.parseInt(line[1])),
                (Point) nodes.elementAt(Integer.parseInt(line[2])),
                (Point) nodes.elementAt(Integer.parseInt(line[3])),
                vw, vh);
        physics();
    }

    public PVector getScreenSpaceCentroid() {
        float[] ret = GLProject.project(middleNode.pos.x,
                middleNode.pos.y,
                middleNode.pos.z);

        return new PVector(ret[0], ret[1], 0);
    }

    public String pickle(Vector<Node> nodes) {
        return "F,"
                + nodes.indexOf(forwardNode) + ","
                + nodes.indexOf(middleNode) + ","
                + nodes.indexOf(topNode) + ",";

    }

    public PVector getCentroid() {
        return middleNode.pos;
    }

        public boolean dependsOn(Point p) {
        if (p == forwardNode ||
                p == middleNode||
                p == topNode) {
            return true;
        } else {
            return false;
        }
    }

}
